#!/bin/bash

if [[ -z "$METIS_HOME" ]]; then
        echo "The METIS_HOME environment variable has not been set."
        echo "This is the directory where metis.sh and metis.config are stored"
        exit 1
fi

source "$METIS_HOME"metis.config

error=false
echo "Performing basic validation checks against: ${METIS_HOME}metis.config:"

if [ -e "$workingDir" ]; then
	if [[ ! "$workingDir" =~ /$ ]];then
		echo "- Variable workingDir does not end with a /"
		error=true
	else
		echo "+ Valid working directory specified"
	fi
fi

if [[ ! "$speed" =~ ^T[0-5]$ ]]; then
	echo "- Invalid nmap timing template defined, valid options are -T[0-5]"
	error=true
else
	echo "+ Valid Nmap timing template defined"
fi

if [[ ! "$threads" =~ ^[0-9]*$ ]]; then
	echo "- Invalid value specified for \"threads\" variable"
	error=true
else
	echo "+ Valid number of threads specified"
fi

if [[ ! "$scanLimit" =~ ^[0-9]*$ ]]; then
	echo "- Invalid value specified for the scanLimit variable"
	error=true
else
	echo "+ Valid scanLimit specified"	
fi

if [[ "$tcp_shotgun_ports" =~ ^[1-9][0-9]*(,[1-9][0-9]*)*$ ]]; then
	echo "+ Valid TCP shotgun port range specified"
else
	echo "- Invalid TCP shotgun port range specified. Value must be of the form \"22,500,6500\", and no numbers can start with a zero."
	error=true
fi

if [[ "$udp_shotgun_ports" =~ ^[1-9][0-9]*(,[1-9][0-9]*)*$ ]]; then
	echo "+ Valid UDP shotgun port range specified"
else
	echo "- Invalid UDP shotgun port range specified. Value must be of the form \"22,500,6500\", and no numbers can start with a zero."
	error=true
fi

correctLog=false
case "$logginglevel" in 
"debug")correctLog=true;;
"info")correctLog=true;;
"warning")correctLog=true;;
"error")correctLog=true;;
"critical")correctLog=true;;
esac

if [ "$correctLog" == true ]; then
	echo "+ Valid debug level specified"
else
	echo "+ Invalid value specified for the logginglevel variable"
	error=true
fi

if [ "$error" == true ]; then
	exit 1
else
	exit 0
fi
